from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
# 用户模型
class UserModel(AbstractUser):
    nickname = models.CharField(max_length=255, verbose_name='昵称', null=True, blank=True)
    avatar = models.ImageField(upload_to='user/avatar/%Y', verbose_name='头像', null=True, blank=True)
    phone = models.CharField(max_length=11, blank=True, null=True, verbose_name='手机号')

    class Meta:
        db_table = "user_model"
        verbose_name = "用户"
        verbose_name_plural = "用户"

    def __str__(self):
        return self.phone


#  问医生-9个分类
class HealthInfoCategory(models.Model):
    title = models.CharField(max_length=255, verbose_name="标题")
    description = models.TextField(verbose_name="描述")
    link = models.URLField(max_length=255, verbose_name="链接", null=True, blank=True)

    def __str__(self):
        return self.title

    class Meta:
        db_table = "health_info_category"
        verbose_name = "健康信息分类"
        verbose_name_plural = "健康信息分类"


# 大健康和生活手册
class HealthCategory(models.Model):
    name = models.CharField(max_length=255, verbose_name="名称")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "health_category"
        verbose_name = "健康类别"
        verbose_name_plural = "健康类别"


#   大健康和生活手册-一级类
class SubCategory(models.Model):
    category = models.ForeignKey(HealthCategory, on_delete=models.CASCADE, related_name="sub_categories")
    name = models.CharField(max_length=255, verbose_name="名称")

    def __str__(self):
        return self.name

    class Meta:
        db_table = "sub_category"
        verbose_name = "子类别"
        verbose_name_plural = "子类别"


# 大健康和生活手册-二级类
class HealthItem(models.Model):
    sub_category = models.ForeignKey(SubCategory, on_delete=models.CASCADE, related_name="items")
    name = models.CharField(max_length=255, verbose_name="名称")
    description = models.TextField(verbose_name="描述", blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = "health_item"
        verbose_name = "健康项目"
        verbose_name_plural = "健康项目"


# 医院模型
class Hospital(models.Model):
    name = models.CharField(max_length=255, verbose_name="医院名称")
    address = models.CharField(max_length=255, verbose_name="医院地址")
    # 是否推荐
    is_recommend = models.BooleanField(default=False, verbose_name="是否推荐", blank=True, null=True)
    departments = models.ManyToManyField('Department', related_name="hospitals", verbose_name="所属科室",
                                         blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'hospital'
        verbose_name = "医院"
        verbose_name_plural = "医院"


# 科室模型
class Department(models.Model):
    name = models.CharField(max_length=255, verbose_name="科室名称")
    url = models.CharField(max_length=255, verbose_name="科室链接", null=True, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'department'
        verbose_name = "科室"
        verbose_name_plural = "科室"


# 医生模型
class Doctor(models.Model):
    name = models.CharField(max_length=255, verbose_name="医生姓名")
    title = models.CharField(max_length=100, verbose_name="职称")
    hospital = models.ForeignKey(Hospital, on_delete=models.CASCADE, verbose_name="所属医院")
    primary_department = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True, blank=True,
                                           related_name="doctors",
                                           verbose_name="主要科室")  # 主要科室的外键
    department = models.ManyToManyField(Department,
                                        related_name="other_departments",
                                        verbose_name="所属科室")  # 多对多关系
    score = models.FloatField(verbose_name="评分")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="咨询费用")
    response_count = models.IntegerField(verbose_name="回答数量")
    description = models.TextField(verbose_name="医生简介")
    avatar = models.ImageField(upload_to='doctor/avatar/%Y/%m', verbose_name="头像", null=True, blank=True)
    is_recommend = models.BooleanField(default=False, verbose_name="是否推荐", blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'doctor'
        verbose_name = "医生"
        verbose_name_plural = "医生"


# 用户咨询
class UserQuestion(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="用户")
    doctor = models.ForeignKey(Doctor, on_delete=models.SET_NULL, null=True, verbose_name="咨询医生")
    title = models.CharField(max_length=255, verbose_name="问题标题")
    content = models.TextField(verbose_name="问题内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):

        return "用户咨询"

    class Meta:
        db_table = 'user_question'
        verbose_name = "用户咨询"
        verbose_name_plural = "用户咨询"


# 医生回复
class DoctorResponse(models.Model):
    question = models.ForeignKey(UserQuestion, on_delete=models.CASCADE, verbose_name="问题")
    doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE, verbose_name="回复医生")
    content = models.TextField(verbose_name="回复内容")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="回复时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return f"{self.doctor.name}'s response to {self.question.title}"

    class Meta:
        db_table = 'doctor_response'
        verbose_name = "医生回复"
        verbose_name_plural = "医生回复"


# 药物模型
class Medicine(models.Model):
    name = models.CharField(max_length=255, verbose_name="药品名称")
    generic_name = models.CharField(max_length=255, verbose_name="通用名称")
    description = models.TextField(verbose_name="药品描述")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="价格")
    manufacturer = models.CharField(max_length=255, verbose_name="生产厂家")
    dosage = models.CharField(max_length=255, verbose_name="用法用量")
    is_recommend = models.BooleanField(default=False, verbose_name="是否推荐", blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'medicine'
        verbose_name = "药品"
        verbose_name_plural = "药品"


# 将医生-医院-科室-药品名字
class DoctorHospitalMedicine(models.Model):
    recommend = models.CharField(max_length=255, verbose_name="推荐")
    doctor = models.CharField(max_length=255, verbose_name="医生")
    hospital = models.CharField(max_length=255, verbose_name="医院")
    medicine = models.CharField(max_length=255, verbose_name="药品")

    class Meta:
        db_table = 'doctor_hospital_medicine'
        verbose_name = "医生-医院-科室-药品"
        verbose_name_plural = "医生-医院-科室-药品"

    def __str__(self):
        return self.recommend


class Video(models.Model):
    video_file = models.FileField(upload_to='videos/')
    uploaded_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'video'
        verbose_name = "视频"
        verbose_name_plural = "视频"

    def __str__(self):
        return self.video_file


# 患者模型
class Patient(models.Model):
    patientName = models.CharField(default='', max_length=255, verbose_name="患者姓名", null=True, blank=True)
    gender = models.CharField(default='', max_length=255, verbose_name="性别", null=True, blank=True)
    birthDate = models.DateField(default='', verbose_name="出生日期", null=True, blank=True)
    weight = models.FloatField(default=50, verbose_name="体重", null=True, blank=True)
    allergyHistory = models.TextField(default='', verbose_name="过敏史", null=True, blank=True)
    pastMedicalHistory = models.TextField(default='', verbose_name="既往病史", null=True, blank=True)
    liverFunction = models.CharField(default='', max_length=255, verbose_name="肝功能", null=True, blank=True)
    kidneyFunction = models.CharField(default='', max_length=255, verbose_name="肾功能", null=True, blank=True)
    pregnancyStatus = models.CharField(default='', max_length=255, verbose_name="怀孕情况", null=True, blank=True)
    user = models.IntegerField(verbose_name="医生查看患者信息")

    class Meta:
        db_table = 'patient'
        verbose_name = "患者"
        verbose_name_plural = "患者"
